using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace Ferpect.GameState.MenuItems
{
    [DefaultProperty("Label")]
    [Description("A menu item with an adjustable integer count.")]
    public class CounterMenuItem : MenuItem
    {
        private int count;
        private int increment = 1;
        private string label;
        private int minimum;
        private int maximum = 10;
        private string text;
        private const string Format = "{0}: {1}";

        public CounterMenuItem()
        {
            this.label = "Label";
            this.text = String.Format(Format, this.label, this.count);
        }

        [Category("Appearance")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int Count
        {
            get { return this.count; }
            set
            {
                if (this.count != value)
                {
                    this.count = value;
                    if (this.count > this.maximum)
                    {
                        this.maximum = this.count;
                    }
                    if (this.count < this.minimum)
                    {
                        this.minimum = this.count;
                    }
                    this.text = String.Format(Format, this.label, this.count);
                }
            }
        }

        [Category("Behavior")]
        [DefaultValue(1)]
        public int Increment
        {
            get { return this.increment; }
            set
            {
                this.increment = value;
            }
        }

        [Category("Appearance")]
        public string Label
        {
            get { return label; }
            set
            {
                if (this.label != value)
                {
                    this.label = value;
                    this.text = String.Format(Format, this.label, this.count);
                }
            }
        }

        [Category("Behavior")]
        [DefaultValue(0)]
        [RefreshProperties(RefreshProperties.All)]
        public int Minimum
        {
            get { return this.minimum; }
            set
            {
                this.minimum = value;
                if (this.minimum > this.maximum)
                {
                    this.maximum = this.minimum;
                }
                if (this.count < this.minimum)
                {
                    this.Count = this.minimum;
                }
            }
        }

        [Category("Behavior")]
        [DefaultValue(10)]
        [RefreshProperties(RefreshProperties.All)]
        public int Maximum
        {
            get { return this.maximum; }
            set
            {
                this.maximum = value;
                if (this.maximum < this.minimum)
                {
                    this.minimum = this.maximum;
                }
                if (this.count > this.maximum)
                {
                    this.Count = this.maximum;
                }
            }
        }

        public override string GetText()
        {
            return this.text;
        }

        protected internal override void OnSelected()
        {
            if ((this.maximum - this.count) >= this.increment)
            {
                this.count = this.count + this.increment;
            }
            else
            {
                this.count = minimum;
            }
            this.text = String.Format(Format, this.label, this.count);

            base.OnSelected();
        }
    }
}
